package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.ActivityData;

import java.util.List;
import java.util.Locale;

public class ActivityRankingAdapter extends RecyclerView.Adapter<ActivityRankingAdapter.ViewHolder> {

    private final Context context;
    private List<ActivityData> rankingList;
    private final String currentUsername;

    public ActivityRankingAdapter(Context context, List<ActivityData> rankingList, String currentUsername) {
        this.context = context;
        this.rankingList = rankingList;
        this.currentUsername = currentUsername;
    }

    public void updateData(List<ActivityData> newRankingList) {
        this.rankingList = newRankingList;
        notifyDataSetChanged();
    }
    
    public void setData(List<ActivityData> newRankingList) {
        updateData(newRankingList);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_activity_ranking, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ActivityData activityData = rankingList.get(position);
        
        // 设置排名
        holder.rankNumber.setText(String.valueOf(position + 1));
        
        // 设置用户名
        holder.userName.setText(activityData.getUsername());
        
        // 设置详情和积分
        holder.userDetail.setText(String.format(Locale.getDefault(), 
                "连续登录: %d天", activityData.getConsecutiveLogins()));
        holder.activityPoints.setText(String.format(Locale.getDefault(), 
                "%d分", activityData.getMonthlyActivity()));
        
        // 为前三名设置不同的背景颜色
        if (position == 0) {
            holder.rankNumber.setBackgroundResource(R.drawable.bg_achievement_gold);
        } else if (position == 1) {
            holder.rankNumber.setBackgroundResource(R.drawable.bg_achievement_silver);
        } else if (position == 2) {
            holder.rankNumber.setBackgroundResource(R.drawable.bg_achievement_bronze);
        } else {
            holder.rankNumber.setBackgroundResource(R.drawable.bg_activity_card);
        }
    }

    @Override
    public int getItemCount() {
        return rankingList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView rankNumber;
        TextView userName;
        TextView userDetail;
        TextView activityPoints;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rankNumber = itemView.findViewById(R.id.rank_number);
            userName = itemView.findViewById(R.id.user_name);
            userDetail = itemView.findViewById(R.id.user_detail);
            activityPoints = itemView.findViewById(R.id.activity_points);
        }
    }
} 